package com.xkdx.caipiao.setup;

import java.util.HashMap;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.xkdx.caipiao.R;
import com.xkdx.caipiao.ThreadActivity;
import com.xkdx.caipiao.module.network.AbsModule;
import com.xkdx.caipiao.presistence.changepas.ChangepasAction;
import com.xkdx.caipiao.presistence.changepas.ChangepasModule;
import com.xkdx.caipiao.presistence.changepas.ChangepasPresistence;
import com.xkdx.caipiao.statics.IConstants;
import com.xkdx.caipiao.util.Md5Utils;
import com.xkdx.guangguang.LoginActivity;

public class AlterPasswordActivity extends ThreadActivity {
	ChangepasAction action;
	ChangepasModule module;
	ChangepasPresistence presistence;
	private ImageView back;
	private EditText oldpas;
	private EditText newpas1;
	private EditText newpas2;
	private Button bt_end;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_alter_password_caipiao);
		findView();
	}
	private void findView() {
		oldpas = (EditText) findViewById(R.id.et_old);
		newpas1 = (EditText) findViewById(R.id.et_new1);
		newpas2 = (EditText) findViewById(R.id.et_new2);
	
		

		bt_end = (Button) findViewById(R.id.bt_end);
		bt_end.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				String old = oldpas.getText().toString();
				String pas1 = newpas1.getText().toString();
				String pas2 = newpas2.getText().toString();
				
				if (TextUtils.isEmpty(old)) {
					Toast.makeText(AlterPasswordActivity.this, "旧密码不能为空", 1).show();
					/*Toast toast = Toast.makeText(AlterPasswordActivity.this, "旧密码不能为空", Toast.LENGTH_SHORT); 
					View view = toast.getView(); 
					view.setBackgroundResource(R.drawable.toast); 
					toast.setView(view); 
					toast.show();*/
				/*	Tost.toast(AlterPasswordActivity.this, "旧密码不能为空", null);*/
					return;
				}else
				if (TextUtils.isEmpty(pas1)) {
					Toast.makeText(AlterPasswordActivity.this, "新密码不能为空", 1).show();
					return;
				} else
					if (pas1.length() < 6 ) {
						Toast.makeText(AlterPasswordActivity.this, "请输入6-12位新密码", 1).show();
						return;
					}
				 if (TextUtils.isEmpty(pas2)) {
					Toast.makeText(AlterPasswordActivity.this, "请再次输入新密码", 1).show();
					return;
				}else
				if (pas1.length() < 6 ) {
					Toast.makeText(AlterPasswordActivity.this, "请输入6-12位新密码", 1).show();
					return;
				}
				if (pas1.equals(pas2)) {
					startThread();
					return;
				}else {
					
					Toast.makeText(AlterPasswordActivity.this, "两次密码不一致，请重新输入", 1).show();
				}
				
				
				
			}
		});
		
		back = (ImageView) findViewById(R.id.back);
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}
	public void showOnPost(HashMap<String, AbsModule> result) {
		if (result.containsKey("0")) {
			String str = module.toastInfo;
			Toast.makeText(this, str, 1).show();
			if(str.equals("成功, 系统处理正常")){
				usrSP.setLogintoken("");
				finish();
				Intent intent = new Intent(this, LoginActivity.class);
				startActivity(intent);
			}
		}else{
			Toast.makeText(this, "修改失败", 1).show();	
		}

		}

	@Override
	public void startThread() {
//		LoginInfo info = getLoginInfo();
		String md5 = usrSP.getId()+usrSP.getLogintoken()+IConstants.key;
		String signcode = Md5Utils.md5(md5);
		action = new ChangepasAction(usrSP.getId(), usrSP.getLogintoken(),oldpas.getText().toString(),
				newpas1.getText().toString(),signcode);
		module = new ChangepasModule();
		presistence = new ChangepasPresistence(this);
		presistence.setActitons(action);
		presistence.setModule(module);
		presistence.execute();
	}
	@Override
	public void onBackClick(View view) {
		// TODO Auto-generated method stub
		
	}

}
